from utils.aes_util import aes_util
from utils.log_util import logger
from utils.read_excel import read_excel_by_file
from utils.str_util import get_birthday
from utils.write_util import write_json_to_txt


def make_tar_files(file_name, month_list, dir_name="data_files"):
    data_file = read_excel_by_file(file_name, dir_name)
    amount = 288
    lc_totalRecord = data_file["lc_totalRecord_test"]
    uc_totalRecord = data_file["uc_totalRecord_test"]
    for month in month_list:
        logger.info(month)
        lc_total = []
        lc_num_total = 0
        lc_amount_total = 0
        uc_total = []
        uc_num_total = 0
        uc_amount_total = 0
        for row in lc_totalRecord.iter_rows(min_row=2, values_only=True):
            if row[0] == month:
                logger.info(row)
                number = 0
                amountTotal = 0
                queryDetail = []
                lc_queryDetail = data_file[f"{row[1]}_lc_queryDetail_test"]
                for row1 in lc_queryDetail.iter_rows(min_row=2, values_only=True):
                    if row1[0] == month:
                        logger.info(row1)
                        number += 1
                        real_amount = 0
                        if str(row1[1]) == "0":
                            real_amount = amount
                            amountTotal += amount
                        queryDetail.append({
                            "seq": 1,
                            "reason": str(row1[1]),
                            "nachn": row1[3],
                            "pgroupCode": "A",
                            "idCode": row1[4],
                            "idCard": aes_util.aes_en_code(row1[5]),
                            "sexCode": row1[6],
                            "gbdat": aes_util.aes_en_code(get_birthday(row1[5])),
                            "bankCard": aes_util.aes_en_code(row1[7]),
                            "bankName": row1[8],
                            "insuranceArea": row1[9],
                            "insuranceCode": row1[10],
                            "insuranceName": row1[11],
                            "orgCode1": None,
                            "orgName1": None,
                            "orgCode2": None,
                            "orgName2": None,
                            "orgCode3": None,
                            "orgName3": None,
                            "pernr": row1[2],
                            "changeDate": "2024/02/01",
                            "amount": real_amount,
                            "listedFlag": "X",
                            "insureNumber": None,
                            "perStatus": row1[12]
                        })
                lc_total.append({
                    "orgCode": row[1],
                    "orgName": row[2],
                    "numTotal": number,
                    "amountTotal": aes_util.aes_en_code(amountTotal),
                    "isHealth": "1",
                    "listedFlag": "X",
                    "healthText": "Y"
                })
                lc_num_total += number
                lc_amount_total += amountTotal
                queryDetail_json = {
                    "code": "200",
                    "message": None,
                    "data": queryDetail
                }
                write_json_to_txt(f"MSP209\\{month}", f"{row[1]}_lc_queryDetail_test", queryDetail_json)
        lc_total_json = {
            "code": "200",
            "message": None,
            "data": {
                "insurePerInfoExportVOList": lc_total,
                "numTotal": lc_num_total,
                "amountTotal": lc_amount_total
            }
        }
        write_json_to_txt(f"MSP209\\{month}", "lc_totalRecord_test", lc_total_json)

        for row in uc_totalRecord.iter_rows(min_row=2, values_only=True):
            if row[0] == month:
                logger.info(row)
                number = 0
                amountTotal = 0
                queryDetail = []
                uc_queryDetail = data_file[f"{row[1]}_uc_queryDetail_test"]
                for row1 in uc_queryDetail.iter_rows(min_row=2, values_only=True):
                    if row1[0] == month:
                        logger.info(row1)
                        number += 1
                        real_amount = 0
                        if str(row1[1]) == "0":
                            real_amount = amount
                            amountTotal += amount
                        queryDetail.append({
                            "seq": 1,
                            "reason": str(row1[1]),
                            "nachn": row1[3],
                            "pgroupCode": "A",
                            "idCode": row1[4],
                            "idCard": aes_util.aes_en_code(row1[5]),
                            "sexCode": row1[6],
                            "gbdat": aes_util.aes_en_code(get_birthday(row1[5])),
                            "bankCard": aes_util.aes_en_code(row1[7]),
                            "bankName": row1[8],
                            "insuranceArea": row1[9],
                            "insuranceCode": row1[10],
                            "insuranceName": row1[11],
                            "orgCode1": None,
                            "orgName1": None,
                            "orgCode2": None,
                            "orgName2": None,
                            "orgCode3": None,
                            "orgName3": None,
                            "pernr": row1[2],
                            "changeDate": "2024/02/01",
                            "amount": real_amount,
                            "listedFlag": "X",
                            "insureNumber": None,
                            "perStatus": row1[12]
                        })
                uc_total.append({
                    "orgCode": row[1],
                    "orgName": row[2],
                    "numTotal": number,
                    "amountTotal": aes_util.aes_en_code(amountTotal),
                    "isHealth": "1",
                    "listedFlag": "",
                    "healthText": "Y"
                })
                uc_num_total += number
                uc_amount_total += amountTotal
                queryDetail_json = {
                    "code": "200",
                    "message": None,
                    "data": queryDetail
                }
                write_json_to_txt(f"MSP209\\{month}", f"{row[1]}_uc_queryDetail_test", queryDetail_json)
        uc_total_json = {
            "code": "200",
            "message": None,
            "data": {
                "insurePerInfoExportVOList": uc_total,
                "numTotal": uc_num_total,
                "amountTotal": uc_amount_total
            }
        }
        write_json_to_txt(f"MSP209\\{month}", "uc_totalRecord_test", uc_total_json)
